<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>JXPath</title><link rel="stylesheet" type="text/css" href="eclipse_book.css"><meta name="generator" content="DocBook XSL Stylesheets V1.77.1"><link rel="home" href="index.html" title="Red Hat Fuse Tooling"><link rel="up" href="RiderExLang.html" title="Part&nbsp;III.&nbsp;Expression and Predicates Languages"><link rel="prev" href="JsonPath.html" title="JsonPath"><link rel="next" href="MVEL.html" title="MVEL"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="JXPath"></a>JXPath</h2></div></div></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-34047"></a>Overview</h2></div></div></div><p>The JXPath language enables you to invoke Java beans using the <a class="link" href="http://commons.apache.org/jxpath/" target="_top">Apache Commons JXPath</a> language. The
JXPath language has a similar syntax to XPath, but instead of selecting element or attribute
nodes from an XML document, it invokes methods on an object graph of Java beans. If one of the
bean attributes returns an XML document (a DOM/JDOM instance), however, the remaining portion
of the path is interpreted as an XPath expression and is used to extract an XML node from the
document. In other words, the JXPath language provides a hybrid of object graph navigation and
XML node selection.</p></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-34048"></a>Adding JXPath package</h2></div></div></div><p>To use JXPath in your routes you need to add a dependency on
<code class="literal">camel-jxpath</code> to your project as shown in <a class="xref" href="JXPath.html#JXPath-ExDEP" title="Example&nbsp;8.&nbsp;Adding the camel-jxpath dependency">Example&nbsp;8, &#8220;Adding the camel-jxpath dependency&#8221;</a>.</p><div class="example"><a name="JXPath-ExDEP"></a><p class="title"><b>Example&nbsp;8.&nbsp;Adding the camel-jxpath dependency</b></p><div class="example-contents"><pre class="screen">&lt;!-- Maven POM File --&gt;
&lt;properties&gt;
  &lt;camel-version&gt;2.21.0.fuse-730078-redhat-00001&lt;/camel-version&gt;
  ...
&lt;/properties&gt;

&lt;dependencies&gt;
  ...
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-jxpath&lt;/artifactId&gt;
    &lt;version&gt;${camel-version}&lt;/version&gt;
  &lt;/dependency&gt;
  ...
&lt;/dependencies&gt;</pre></div></div><br class="example-break"></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-34049"></a>Variables</h2></div></div></div><p><a class="xref" href="JXPath.html#JXPath-TableVARS" title="Table&nbsp;9.&nbsp;JXPath variables">Table&nbsp;9, &#8220;JXPath variables&#8221;</a> lists the variables that are accessible when using
JXPath.</p><div class="table"><a name="JXPath-TableVARS"></a><p class="title"><b>Table&nbsp;9.&nbsp;JXPath variables</b></p><div class="table-contents"><table summary="JXPath variables" border="1"><colgroup><col width="33%" class="col_1"><col width="33%" class="col_2"><col width="34%" class="col_3"></colgroup><thead><tr><th align="left" valign="top">Variable</th><th align="left" valign="top">Type</th><th align="left" valign="top">Value</th></tr></thead><tbody><tr><td align="left" valign="top"><p><code class="literal">this</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.Exchange</code></p></td><td align="left" valign="top"><p>The current Exchange</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">in</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.Message</code></p></td><td align="left" valign="top"><p>The IN message</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">out</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.Message</code></p></td><td align="left" valign="top"><p>The OUT message</p></td></tr></tbody></table></div></div><br class="table-break"></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-jxpath-options"></a>Options</h2></div></div></div><p><a class="xref" href="JXPath.html#jxpath-tableoptions" title="Table&nbsp;10.&nbsp;JXPath option">Table&nbsp;10, &#8220;JXPath option&#8221;</a> describes the option for JXPath.</p><div class="table"><a name="jxpath-tableoptions"></a><p class="title"><b>Table&nbsp;10.&nbsp;JXPath option</b></p><div class="table-contents"><table summary="JXPath option" border="1"><colgroup><col width="33%" class="col_1"><col width="33%" class="col_2"><col width="34%" class="col_3"></colgroup><thead><tr><th align="left" valign="top">Option</th><th align="left" valign="top">Type</th><th align="left" valign="top">Description</th></tr></thead><tbody><tr><td align="left" valign="top"><p><code class="literal">lenient</code></p></td><td align="left" valign="top"><p><code class="literal">boolean</code></p></td><td align="left" valign="top"><p>Camel 2.11/2.10.5: Allows to turn lenient on the JXPathContext. When turned on this option allows the JXPath expression to evaluate against expressions and message bodies which might be invalid or missing data. See more details at the <a class="link" href="http://commons.apache.org/proper/commons-jxpath//users-guide.html#Lenient_Mode" target="_top">JXPath Documentation</a>. This option is false, by default.</p></td></tr></tbody></table></div></div><br class="table-break"></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-34050"></a>Examples</h2></div></div></div><p>The following example route uses JXPath:</p><div class="informalexample"><pre class="screen">&lt;camelContext&gt;
  &lt;route&gt;
    &lt;from uri="activemq:MyQueue"/&gt;
    &lt;filter&gt;
      &lt;jxpath&gt;in/body/name = 'James'&lt;/xpath&gt;
      &lt;to uri="mqseries:SomeOtherQueue"/&gt;
    &lt;/filter&gt;
  &lt;/route&gt;
&lt;/camelContext&gt;</pre></div><p>The following simple example uses a JXPath expression as a predicate in a Message Filter:</p><div class="informalexample"><pre class="screen">{snippet:id=example|lang=java|url=camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterTest.java}</pre></div></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-jxpathinjection"></a>JXPath injection</h2></div></div></div><p>You can use Bean Integration to invoke a method on a bean and use various languages, such as JXPath, to extract a value from the message and bind it to a method parameter.</p><p>For example:</p><pre class="screen">public class Foo {
     @MessageDriven(uri = "activemq:my.queue")
     public void doSomething(@JXPath("in/body/foo") String correlationID, @Body String body)
     { // process the inbound message here }
   }</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-jxpathloadingscript"></a>Loading the script from an external resource</h2></div></div></div><p><span class="emphasis"><em>Available as of Camel 2.11</em></span></p><p>You can externalize the script and have Camel load it from a resource such as <code class="literal">"classpath:"</code>, <code class="literal">"file:"</code>, or <code class="literal">"http:"</code>. Use the following syntax:</p><pre class="screen">"resource:scheme:location"</pre><p>For example, to reference a file on the classpath:</p><pre class="screen">.setHeader("myHeader").jxpath("resource:classpath:myjxpath.txt")</pre></div></div></body></html>